﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PeachLite.Entity;
using PeachLite.Helpers;
using System.IO;

namespace PeachLite.Admin
{
    public partial class ArticleEdit1 : PageBase
    {
        private const string IMAGES_PATH = "/u/image/";
        private const string TIME_FORMAT = "yyyyMMddHHmmssfff";
        private static string[] IMAGE_EXTENSION = { ".jpg", ".jpeg", ".png", "gif" };
        protected override void Do()
        {
            base.Do();

            if (!IsPostBack)
            {
                SysMessage m = new SysMessage();
                string id = Request.QueryString["aid"] == null ? "" : Request.QueryString["aid"];
                //检查文章编号是否为int型
                if (id != "")
                {
                    try
                    {
                        id = int.Parse(id).ToString();
                    }
                    catch
                    {
                        ShowMessage(1, "文章编号数据类型有误", "文章编号数据类型有误", null);
                        return;
                    }
                }

                string sql = "sp_Admin_Page_GetArticle '" + id + "'";

                SysResult r = SqlHelper.Execute(sql);
                string cid = "";
                if (!r.Success)
                {
                    ShowMessage(r.Code, r.Message, r.Message, null);
                    return;
                }

                //如果文章编号不为空,填数据
                if (!string.IsNullOrWhiteSpace(id))
                {
                    articleId.Value = r.Data["ID"].ToString();
                    articleTitle.Value = r.Data["Title"].ToString();
                    titleSub.Value = r.Data["TitleSub"].ToString();
                    titleImage.Src = string.IsNullOrWhiteSpace(r.Data["TitleImage"].ToString()) ? "resx/img/none_80.jpg" : r.Data["TitleImage"].ToString();
                    ordinal.Value = r.Data["Ordinal"].ToString();
                    author.Value = r.Data["Author"].ToString();
                    source.Value = r.Data["Source"].ToString();
                    createTime.Value = r.Data["CreateTime"].ToString();
                    redirectUrl.Value = r.Data["RedirectUrl"].ToString();
                    keyword.Value = r.Data["MetaKeywords"].ToString();
                    description.Value = r.Data["MetaDescription"].ToString();
                    summary.Value = r.Data["Summary"].ToString();
                    content.Value = r.Data["AContent"].ToString();
                    cid = r.Data["ClassID"].ToString();
                }
                if (r.DSet.Tables.Count > 0)
                {
                    for (int i = 0; i < r.DSet.Tables[0].Rows.Count; i++)
                    {
                        ListItem item = new ListItem(r.DSet.Tables[0].Rows[i][1].ToString(), r.DSet.Tables[0].Rows[i][0].ToString());
                        if (r.DSet.Tables[0].Rows[i][0].ToString() == cid)
                        {
                            item.Selected = true;
                        }
                        classId.Items.Add(item);
                    }
                }
            }
        }

        protected void saveArticle_Click(object sender, EventArgs e)
        {
            string aid = "";
            int cid;
            string tit;
            string tsub;
            string timg;
            int ord;
            string aut;
            string sour;
            string ctime;
            string rurl;
            string key;
            string desc;
            string sumy;
            string cont;
            int t;
            if (!string.IsNullOrWhiteSpace(articleId.Value.Trim()))
            {
                try
                {
                    aid = int.Parse(articleId.Value).ToString();
                }

                catch
                {
                    ShowMessage(1, "文章编号数据类型有误", "文章编号数据类型有误", null);
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(articleTitle.Value))
            {
                ShowMessage(1, "文章标题不能为空", "文章标题不能为空", null);
                return;
            }
            tit = articleTitle.Value;
            tsub = titleSub.Value == null ? "" : titleSub.Value;
            timg = titImgPath.Value == null ? "" : titImgPath.Value;
            ord = string.IsNullOrWhiteSpace(ordinal.Value) ? 1 : int.TryParse(ordinal.Value, out t) ? t : 1;
            aut = author.Value == null ? "" : author.Value;
            sour = source.Value == null ? "" : source.Value;
            ctime = createTime.Value == null ? "" : createTime.Value;
            rurl = redirectUrl.Value == null ? "" : redirectUrl.Value;
            key = keyword.Value == null ? "" : keyword.Value;
            desc = description.Value == null ? "" : description.Value;
            sumy = summary.Value == null ? "" : summary.Value;
            cont = content.Value == null ? "" : content.Value;
            cid = classId.SelectedIndex < 0 ? int.Parse(classId.Items[0].Value) : int.Parse(classId.Items[classId.SelectedIndex].Value);

            string sql = "sp_Admin_Ajax_SaveArticle '" + aid + "'," + cid + ",'" + tit + "','" + tsub + "','" + timg + "'," + ord + ",'" + aut + "','" + sour + "','" + ctime + "','" + rurl + "','" + key + "','" + desc + "','" + sumy + "','" + cont + "'," + Admin.AID.ToString();
            SysResult r = SqlHelper.Execute(sql);
            if (r.Success)
            {
                ShowMessage(1, r.Message, r.Message, null);
                articleId.Value = r.Data["AID"].ToString();
                titleImage.Src = string.IsNullOrWhiteSpace(timg) ? "resx/img/none_80.jpg" : timg;
                return;
            }
            else
            {
                ShowMessage(1, r.Message, r.Message, null);
                return;
            }
        }
    }
}